package com.example.dessert.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.dessert.dao.OrderDetailMapper;
import com.example.dessert.dao.OrdersMapper;
import com.example.dessert.entity.Cart;
import com.example.dessert.entity.OrderDetail;
import com.example.dessert.entity.Orders;
import com.example.dessert.service.CartService;
import com.example.dessert.service.OrderDetailService;
import com.example.dessert.service.OrderService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName: OrderServiceImpl
 * @Description:
 * @Author
 * @Date 2022/1/18
 * @Version 1.0
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrdersMapper ordersMapper;
    @Resource
    private OrderDetailMapper orderDetailMapper;
    @Resource
    private OrderDetailService orderDetailService;
    @Resource
    private CartService cartService;





    @Override
    public Long buyByProduct(Long userId,String productName,String specName,Integer price,Integer number,String contact,String mobile,String province,String city,String county,String address){

        String orderNumber = "D" + System.currentTimeMillis();

        Integer totalPrice = price * number;

        Orders orders = new Orders();
        orders.setUserId(userId)
             .setOrderNumber(orderNumber)
             .setTotalPrice(totalPrice)
             .setContact(contact)
             .setMobile(mobile)
             .setProvince(province)
             .setCity(city)
             .setCounty(county)
             .setAddress(address);
        ordersMapper.insert(orders);
        Long orderId = orders.getId();

        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setOrderId(orderId)
                .setProductName(productName)
                .setSpecName(specName)
                .setPrice(price)
                .setNumber(number);
        orderDetailMapper.insert(orderDetail);

        return  orderId;
    }

    @Override
    public Orders queryOrderById(Long id){

        return ordersMapper.selectById(id);

    }

    @Override
    public int saveOrder(Orders orders){

        return ordersMapper.updateById(orders);

    }

    @Override
    public List<Orders> queryOrderPageByUser(int startPage, int pageSize, Long userId, int status){

        List<Orders> orders =ordersMapper.queryOrderPageByUser(startPage, pageSize, userId, status);
        if (orders.size() > 0) {
            for (Orders order : orders) {
                Long orderId = order.getId();
                List<OrderDetail> orderDetailList = orderDetailService.queryOrderDetailList(orderId);
                order.setOrderDetailList(orderDetailList);
            }
        }
        return orders;
    }

    @Override
    public Long queryOrderCountByUser(Long userId, int status){
        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        if(userId != null){
            wrapper.eq("user_id",userId);
        }
        if(status >= 0){
            wrapper.eq("status",status);
        }
        return ordersMapper.selectCount(wrapper);
    }

    @Override
    public Orders queryOrderByIdAndUserId(Long userId,Long id){

        QueryWrapper<Orders> wrapper = new QueryWrapper<>();
        wrapper.eq("id",id);
        wrapper.eq("user_id",userId);
        Orders order = ordersMapper.selectOne(wrapper);

        Long orderId = order.getId();
        List<OrderDetail> orderDetailList = orderDetailService.queryOrderDetailList(orderId);
        order.setOrderDetailList(orderDetailList);

        return order;
    }

    @Override
    public Long batchBuyByProduct(Long userId,String cartIds,String contact,String mobile,String province,String city,String county,String address){

        String orderNumber = "D" + System.currentTimeMillis();

        List<Cart> carts= cartService.queryCartListByIds(userId,cartIds);

        int totalPrice =0;
        for (Cart cart : carts) {
            int price = cart.getSpecPrice() * cart.getNumber();
            totalPrice = totalPrice + price;
        }

        Orders orders = new Orders();
        orders.setUserId(userId)
                .setOrderNumber(orderNumber)
                .setTotalPrice(totalPrice)
                .setContact(contact)
                .setMobile(mobile)
                .setProvince(province)
                .setCity(city)
                .setCounty(county)
                .setAddress(address);
        ordersMapper.insert(orders);
        Long orderId = orders.getId();


        for (Cart cart : carts) {
            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderId)
                    .setProductName(cart.getProductName())
                    .setSpecName(cart.getSpecName())
                    .setPrice(cart.getSpecPrice())
                    .setNumber(cart.getNumber());
            orderDetailMapper.insert(orderDetail);

        }

        //清除购物车

        return  orderId;

    }

}
